package com.ujn.zhxy.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ujn.zhxy.pojo.Admin;
import com.ujn.zhxy.service.AdminService;
import com.ujn.zhxy.util.MD5;
import com.ujn.zhxy.util.Result;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //注意 不是@Controller ，异步交互的方法
@RequestMapping("/sms/adminController")
public class AdminController {

    @Autowired
    private AdminService adminService;

    //getAllAdmin
    @ApiOperation("查询管理员信息")
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(
            @PathVariable("pageNo") @ApiParam("页码数") Integer pageNo,
            @PathVariable("pageSize") @ApiParam("页大小") Integer pageSize,
            @ApiParam("传入的条件admin的name") String adminName
    ){
        Page<Admin> pageParam = new Page<>(pageNo,pageSize);
        IPage<Admin> adminRs = adminService.getAdminsByOpr(pageParam,adminName);
        return Result.ok(adminRs);
    }

    @ApiOperation("添加或修改管理员信息")
    @PostMapping("/saveOrUpdateAdmin")
    public Result saveOrUpdateAdmin(
            @ApiParam("前端传入的json格式的admin信息") @RequestBody Admin admin
    ){
        Integer id = admin.getId();
        if (id == null || 0 == id) {
            admin.setPassword(MD5.encrypt(admin.getPassword()));
        }
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }

    @ApiOperation("删除单个或多个管理员信息")
    @GetMapping("/deletAdmin")
    public Result saveOrUpdateAdmin(
            @ApiParam("要删除的管理的多个id的json") @RequestBody List<Integer> ids
    ){
        adminService.removeByIds(ids);
        return Result.ok();
    }
}
